<?php

namespace App\Process;

use App\Utility\Cache;
use Carbon\Carbon;
use EasySwoole\Component\Process\AbstractProcess;
use EasySwoole\EasySwoole\Logger;

class CountInc extends AbstractProcess
{
    private $isRun = false;
    public function run($arg){
        //定时500ms检测有没有任务，有的话就while死循环执行
        $this->addTick(200,function (){
            if(!$this->isRun){
                $this->isRun = true;
                go(function (){
                    while (true){
                        try{
                            $queue = new Cache();
                            $task = $queue->onQueue('count-inc')->read();
                            if($task){
                                if($task['applet_type'] == 'video'){
                                    $appletNameModel = 'App\Model\Videos'.ucfirst($task['applet']).'Model';
                                }else{
                                    $appletNameModel = 'App\Model\Articles'.ucfirst($task['applet']).'Model';
                                }
                                $appletModel = new $appletNameModel;
                                $appletCountName = 'App\Model\Counts'.ucfirst($task['applet']).'Model';
                                $countModel = new $appletCountName;
                                $countInfo = $countModel->getCurrentDateInfo($task['id']);
                                if($countInfo){
                                    //判断是否有效分享用户
                                    if($task['share'] && $task['openid']){
                                        $appletCountsLogName = 'App\Model\CountsLog'.ucfirst($task['applet']).'Model';
                                        $countLogModel = new $appletCountsLogName;
                                        //判断是否记录当前用户当日首次进入
                                        if(!$countLogModel->getCurrentLogDateInfo($countInfo['id'],$task['openid'])){
                                            $countLogModel->create(['count_id'=>$countInfo['id'],'openid'=>$task['openid'],'created_at'=>Carbon::now()]);
                                            //当前用户当日首次分享记录有效分享次数+1
                                            $countModel->inc($countInfo['id'],'share_in');
                                            $appletModel->inc($task['id'],'share');
                                        };
                                    }
                                    //更新阅读量
                                    $countModel->inc($countInfo['id'],'view');
                                    unset($countLogModel);
                                }else{
                                    //新增
                                    $count = [
                                        'art_id' => $task['id'],
                                        'admin_id' => $task['admin_id'],
                                        'share_in' => $task['share'] ? 1 : 0,
                                        'view' => 1,
                                        'date' => date('Y-m-d',time()),
                                    ];
                                    $countModel->create($count);
                                }

                                //增加当前文章阅读量
                                $appletModel->inc($task['id'],'view');
                                unset($appletModel);
                                unset($countModel);
                            }else{
                                break;
                            }
                            unset($task);
                        }catch (\Throwable $throwable){
                            break;
                        }
                    }
                    $this->isRun = false;
                });
                unset($queue);
            }
        });
    }

    public function onShutDown()
    {
        // TODO: Implement onShutDown() method.
    }

    public function onReceive(string $str, ...$args)
    {
        // TODO: Implement onReceive() method.
    }
}
